package backboneCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Stereotype;

import rulePackage.RuleSetCollection;

public class RuleEngine {

	public void AddRuleSet(String name, org.eclipse.uml2.uml.Class umlClass) {
		
		String Key = name;
		List<RuleSet> Value = new ArrayList<RuleSet>();
		//TODO: For now eArCon can only handle classes and packages, but it shouldn't be too much of a problem to extend it
		//		just remember that a few rules need it to be of a class stereotype!
		//TODO: Add how to set the different ruleSets
		for (org.eclipse.uml2.uml.Stereotype stereotype : umlClass.getAppliedStereotypes()) {
			Value.add(RuleSetCollection.AddRelationshipRuleSet(umlClass));
			if (stereotype.getName().compareToIgnoreCase("Package") == 0) {
				Value.add(RuleSetCollection.AddPackageCompositeRuleSet(umlClass));
				//TODO: Add package specific ruleSet
			} else {
				Value.add(RuleSetCollection.AddAssociationRuleSet(umlClass, stereotype.getName()));
				Value.add(RuleSetCollection.AddAttributeRuleSet(umlClass));
				//TODO: Add non-package specific ruleSet
			}
		}
		if (umlClass.getAppliedStereotypes().isEmpty()) {
			Value.add(RuleSetCollection.AddAssociationRuleSet(umlClass, "Class"));
			Value.add(RuleSetCollection.AddRelationshipRuleSet(umlClass));
		}
		
		GlobalStorage.addToRuleSet(Key, Value);
		
	}

	public void AppyRule(org.eclipse.uml2.uml.NamedElement element) {
		
		String stereotypeName;
		List<RuleSet> appliedRules = new ArrayList<RuleSet>();
		
		for (org.eclipse.uml2.uml.Stereotype stereotype : element.getAppliedStereotypes()) {
			stereotypeName = stereotype.getName();
			List<RuleSet> ruleSetList = GlobalStorage.getRuleSet(stereotypeName);
			if (ruleSetList == null)
				continue;
			 for (RuleSet ruleSet : ruleSetList) {
				appliedRules.add(new RuleSet(ruleSet));
			}
		}
		GlobalStorage.addToSystemRuleSet(element.getName(), appliedRules);		
	}
}
